﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Model;
using SystemFrameWork;
using SystemFrameWork.Enums;

namespace DAL
{
    public class Sys_RoleDAO : EnittyDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sUser"></param>
        /// <returns></returns>
        public Sys_DepartmentRole LoadSys_RoleByID(Sys_User sUser)
        {
            //try
            //{
            //    DataContextManager mgr = new DataContextManager();
            //    return mgr.CurrentDataContext.Sys_DepartmentRole.Where(s => s.RoleID == sUser.Sys_RoleID).FirstOrDefault();
            //}
            //catch (System.Exception ex)
            //{
            //    throw new Exception(ex.Message);
            //}
            return null;
           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sys_User"></param>
        /// <returns></returns>
        public List<Sys_Role> LoadRoleByUser(Sys_User sys_User)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                var query = from sug in mgr.CurrentDataContext.Sys_Role
                            join
                            sugu in mgr.CurrentDataContext.Sys_RoleUser
                            on sug.ID equals sugu.Sys_RoleID
                            where sugu.Sys_UserID == sys_User.ID
                            select sug;


                return query.ToList();
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Sys_Role LoadRoleByID(Guid id)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                var query = from sug in mgr.CurrentDataContext.Sys_Role
                            //join
                            //sugu in mgr.CurrentDataContext.Sys_RoleUser
                            //on sug.ID equals sugu.Sys_RoleID
                            where sug.ID == id
                            select sug;


                return query.FirstOrDefault();
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public Sys_Role LoadRoleByName(string RoleName)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                var query = from sug in mgr.CurrentDataContext.Sys_Role
                            where sug.Name == RoleName
                            select sug;


                return query.FirstOrDefault();
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="Role"></param>
        /// <param name="departmentId"></param>
        /// <param name="functionModuleList"></param>
        /// <returns></returns>
        public List<RoleView> LoadRoleByCondition(RoleView role)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();

                if (role.ID != Guid.Empty)
                {
                    return mgr.CurrentDataContext.RoleView.Where(ugv => ugv.ID == role.ID).ToList();
                }

                List<RoleView> mList = new List<RoleView>();

                int skipNum = (role.MyPageModel.PageIndex - 1) * role.MyPageModel.PageSize;
                int takeNum = role.MyPageModel.PageSize;

                //if (departmentId != null)
                //{
                //    //var query = from sug in mgr.CurrentDataContext.Sys_Role
                //    //            join ud in mgr.CurrentDataContext.Sys_DepartmentRole
                //    //            on sug.ID equals ud.RoleID
                //    //            where ud.DepartmentID == (Guid)departmentId &&
                //    //            sug.Name.Contains(Role.Name)
                //    //            select sug;
                //    var query = from sug in mgr.CurrentDataContext.RoleView
                //                where sug.DepartmentID == (Guid)departmentId &&
                //                sug.RoleName.Contains(Role.Name) &&
                //                sug.RecordStatus == (byte)RecordStatus.RecordActive
                //                select sug;
                //    Role.MyPageModel.TotalRecord = query.Count();
                //    mList = SqlHelper.DataSorting<RoleView>(query, Role.OrderByField, Role.OrderDirection).Skip(skipNum).Take(takeNum).ToList();
                //}
                //else
                //{
                //    var query = from sug in mgr.CurrentDataContext.RoleView
                //                where sug.RoleName.Contains(Role.Name) &&
                //                sug.RecordStatus == (byte)RecordStatus.RecordActive
                //                select sug;
                //    Role.MyPageModel.TotalRecord = query.Count();
                //    mList = SqlHelper.DataSorting<RoleView>(query, Role.OrderByField, Role.OrderDirection).Skip(skipNum).Take(takeNum).ToList();
                //}
                var query = from sug in mgr.CurrentDataContext.RoleView
                            where sug.RecordStatus==(byte)RecordStatus.RecordActive
                            select sug;
                role.MyPageModel.TotalRecord = query.Count();

                if (!string.IsNullOrEmpty(role.RoleName))
                {
                    query = query.Where(r => r.RoleName.Contains(role.RoleName));
                }
                if (!string.IsNullOrEmpty(role.DepartmentName))
                {
                    query = query.Where(r => r.DepartmentName.Contains(role.DepartmentName));
                }
               
                mList = SqlHelper.DataSorting<RoleView>(query, role.OrderByField, role.OrderDirection).Skip(skipNum).Take(takeNum).ToList();
                return mList;
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public List<Guid?> LoadFunctionModuelID(Guid RoleID)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                var query = from q in mgr.CurrentDataContext.Sys_FunctionModuleSys_Role
                            where q.Sys_RoleID == RoleID
                            select q.Sys_FunctionModuleID;
                return query.ToList();
            }
            catch (DataAccesssException ex)
            {
                throw new DataAccesssException(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Role"></param>
        public void Insert(Sys_Role Role)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                mgr.CurrentDataContext.Sys_Role.InsertOnSubmit(Role);
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="functionModule_Group"></param>
        public void Insert(Sys_FunctionModuleSys_Role functionModule_Group)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                mgr.CurrentDataContext.Sys_FunctionModuleSys_Role.InsertOnSubmit(functionModule_Group);
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="departmentGroup"></param>
        public void Insert(Sys_DepartmentRole departmentGroup)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                mgr.CurrentDataContext.Sys_DepartmentRole.InsertOnSubmit(departmentGroup);
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        public void DeleteSys_DepartmentRole(List<Sys_DepartmentRole> list)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                mgr.CurrentDataContext.Sys_DepartmentRole.DeleteAllOnSubmit(list);
            }
            catch (DataAccesssException ex)
            {
                throw new DataAccesssException(ex.Message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        public void DeleteSys_FunctionModuleSys_Role(List<Sys_FunctionModuleSys_Role> list)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                mgr.CurrentDataContext.Sys_FunctionModuleSys_Role.DeleteAllOnSubmit(list);
            }
            catch (DataAccesssException ex)
            {
                throw new DataAccesssException(ex.Message, ex);
            }
        }

        /// <summary>
        /// remove a entity
        /// </summary>
        /// <param name="id"></param>
        /// <param name="editor"></param>
        public void RemoveEntity(Guid id, Guid editor)
        {
            try
            {
                Sys_Role Role = this.LoadRoleByID(id);
                if (Role != null)
                {
                    EntityHelper helper = new EntityHelper();
                    helper.CascadeDeleteEntity(Role, typeof(Sys_Role), editor, (byte)RecordStatus.RecordDeleted);
                }
            }
            catch (Exception ex)
            {
                throw new DataAccesssException(ex.Message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public List<Sys_DepartmentRole> LoadSys_DepartmentRole(Guid RoleId)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                return mgr.CurrentDataContext.Sys_DepartmentRole.Where(sd => sd.RoleID == RoleId).ToList();
            }
            catch (DataAccesssException ex)
            {
                throw new DataAccesssException(ex.Message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public List<Sys_FunctionModuleSys_Role> Load_SysFunctionModuleSys_Role(Guid RoleId)
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                return mgr.CurrentDataContext.Sys_FunctionModuleSys_Role.Where(sf => sf.Sys_RoleID == RoleId).ToList();
            }
            catch (DataAccesssException ex)
            {
                throw new DataAccesssException(ex.Message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<Sys_Role> LoadAllSys_Role()
        {
            try
            {
                DataContextManager mgr = new DataContextManager();
                return mgr.CurrentDataContext.Sys_Role.ToList();
            }
            catch (DataAccesssException ex)
            {
                throw new DataAccesssException(ex.Message, ex);
            }
        }
    }
}
